package com.example.jh352160.surfingtv.guid;


import android.content.Intent;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentTransaction;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.widget.FrameLayout;
import android.widget.ImageView;
import android.widget.TextView;

import com.example.jh352160.surfingtv.Index.MainActivity;
import com.example.jh352160.surfingtv.R;
import com.example.jh352160.surfingtv.category.Category_main;
import com.example.jh352160.surfingtv.mypage.MypageMain;
import com.example.jh352160.surfingtv.mypage.childPage.LoginPage;
import com.example.jh352160.surfingtv.starsight.StarsightMain;

/**
 * Created by jh352160 on 2016/8/2
 */

public class GuidActivity extends AppCompatActivity implements View.OnClickListener, StarsightMain.FragmentListener {

    TextView index, starsight, category, mypage;
    ImageView index_image, starsight_image, category_image, mypage_image;
    FrameLayout frameLayout;
    StarsightMain starsightMain;
    FragmentManager fragmentManager;
    MainActivity mainActivity;
    Category_main category_main;
    MypageMain mypageMain;
    public static int page = 1;
    public static boolean isLogin = false;
    public static String token = "";

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_luncher);

        index = (TextView) findViewById(R.id.index);
        starsight = (TextView) findViewById(R.id.starsight);
        category = (TextView) findViewById(R.id.category);
        mypage = (TextView) findViewById(R.id.mypage);
        index_image = (ImageView) findViewById(R.id.index_image);
        starsight_image = (ImageView) findViewById(R.id.starsight_image);
        category_image = (ImageView) findViewById(R.id.category_image);
        mypage_image = (ImageView) findViewById(R.id.mypage_image);
        frameLayout = (FrameLayout) findViewById(R.id.frameLayout);

        index.setOnClickListener(this);
        starsight.setOnClickListener(this);
        category.setOnClickListener(this);
        mypage.setOnClickListener(this);
        index_image.setOnClickListener(this);
        starsight_image.setOnClickListener(this);
        category_image.setOnClickListener(this);
        mypage_image.setOnClickListener(this);

        fragmentManager = getSupportFragmentManager();

        SharedPreferences pref = getSharedPreferences("data", MODE_PRIVATE);
        isLogin = pref.getBoolean("isLogin", false);
        token = pref.getString("token", "");

    }

    @Override
    public void onClick(View view) {

        FragmentTransaction ft = fragmentManager.beginTransaction();

        if (mainActivity != null) ft.hide(mainActivity);
        if (starsightMain != null) ft.hide(starsightMain);
        if (category_main != null) ft.hide(category_main);
        if (mypageMain != null) ft.hide(mypageMain);

        setUnselected();
        switch (view.getId()) {
            case R.id.index:
            case R.id.index_image:
                index.setSelected(true);
                index_image.setSelected(true);
                if (mainActivity==null) {
                    mainActivity = MainActivity.newInstance();
                    ft.add(R.id.frameLayout, mainActivity);
                }else{
                    ft.show(mainActivity);
                }
                page = 1;
                break;
            case R.id.starsight:
            case R.id.starsight_image:
                starsight.setSelected(true);
                starsight_image.setSelected(true);
                if (starsightMain==null) {
                    starsightMain = StarsightMain.newInstance();
                    ft.add(R.id.frameLayout, starsightMain);
                }else{
                    ft.show(starsightMain);
                }
                page = 2;
                break;
            case R.id.category:
            case R.id.category_image:
                category.setSelected(true);
                category_image.setSelected(true);
                if (category_main==null) {
                    category_main = Category_main.newInstance();
                    ft.add(R.id.frameLayout, category_main);
                }else{
                    ft.show(category_main);
                }
                page = 3;
                break;
            case R.id.mypage:
            case R.id.mypage_image:
                if (isLogin) {
                    mypage.setSelected(true);
                    mypage_image.setSelected(true);
                    if(mypageMain==null) {
                        mypageMain = MypageMain.newInstance();
                        ft.add(R.id.frameLayout, mypageMain);
                    }else{
                        ft.show(mypageMain);
                    }
                    page = 4;
                } else {
                    page = 1;
                    toLogin();
                    index.setSelected(true);
                    index_image.setSelected(true);
                    if (mainActivity==null) {
                        mainActivity = MainActivity.newInstance();
                        ft.add(R.id.frameLayout, mainActivity);
                    }else{
                        ft.show(mainActivity);
                    }
                }
                break;
        }
        ft.commit();
    }

    @Override
    protected void onResume() {
        super.onResume();
        selectPage();
    }

    private void setUnselected() {
        index.setSelected(false);
        starsight.setSelected(false);
        category.setSelected(false);
        mypage.setSelected(false);
        index_image.setSelected(false);
        starsight_image.setSelected(false);
        category_image.setSelected(false);
        mypage_image.setSelected(false);
    }

    public void selectPage() {
        switch (page) {
            case 1:
                index.performClick();
                break;
            case 2:
                starsight.performClick();
                break;
            case 3:
                category.performClick();
                break;
            case 4:
                mypage.performClick();
                break;
        }
    }

    public void toLogin() {
        Intent intent = new Intent(this, LoginPage.class);
        startActivity(intent);
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
    }
}
